package ca.camosun.morsecode;
import java.util.LinkedList;
import java.util.Queue;

public class MorseQueue {
	private Queue<String> outputQueue;
	private static MorseDecoder decoder;
	private String currentMorseLetter;

	public MorseQueue() {
		outputQueue = new LinkedList<String>();
		decoder = new MorseDecoder();
		currentMorseLetter = "";
	}

	public boolean hasNext() {
		return outputQueue.size() > 0;
	}

	public String getNextDecoded() {
		String next = outputQueue.remove();
		return next;
	}

	public void addToken(MorseDecoder.Token singleToken) {
		if(singleToken.equals(MorseDecoder.Token.LONG_PAUSE)) {
			if(currentMorseLetter.equals("")) {
				outputQueue.add(" ");
				return;
			}
			String humanReadable = decoder.decode(currentMorseLetter);
			outputQueue.add(humanReadable);
			outputQueue.add(" ");
			currentMorseLetter = "";
			return;
		}
		
		if (singleToken.equals(MorseDecoder.Token.SHORT_PAUSE)) {
			String humanReadable = decoder.decode(currentMorseLetter);
			outputQueue.add(humanReadable);
			currentMorseLetter = "";
			return;
		}

		currentMorseLetter += singleToken;
	}
}
